const request = require('../../utils/request')

Page({
  data: {},
  onLoad(opt) {
    let devMac = this.formatDevMac(opt.devMac)
    this.setData({
      devMac: devMac
    })
  },

  back() {
    wx.navigateBack({
      delta: 1,
    });
  },

  formatDevMac(devMac) {
    if (!devMac) {
      return ''
    }
    var arr = devMac.split(":");
    let strs = ''
    arr.forEach(str => {
      strs += str
    })
    return strs
  },

  formSubmit(e) {
    let obj = e.detail.value;
    if (this.objIsNull(obj)) {
      request.post('/editDevName', obj).then(res => {
        if (Number(res.code) === 0) {
          this.successResult("成功")
        } else {
          this.errorResult(res.msg)
        }
        console.log(res);
      })
    }
  },

  objIsNull(obj) {
    for (let key in obj) {
      obj[key] = this.removeSpaces(obj[key])
      if (obj[key] === null || obj[key] === '') {
        return this.errorResult('输入正确的参数')
      }
    }
    return true
  },

  removeSpaces(input) {
    return input.replace(/\s/g, '');
  },

  errorResult(errorInfo) {
    wx.showToast({
      title: errorInfo,
      icon: 'error',
      duration: 2000
    })
  },
  successResult(successInfo) {
    wx.showToast({
      title: successInfo,
      icon: 'success',
      duration: 2000
    })
  }
})